﻿# region Using Directives

using System;
using PMSytem.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

using System.Windows.Threading;
using System.Threading;
using System.Windows.Input;
using PMSytem.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
using PMSystem.ApplicationLogics;
using PMSytem.Windows.App;

# endregion

namespace PMSytem.Windows.Tests
{
	public partial class Login : Page
	{
		#region Constructor
		UserService _service;
		Window mainwindow, loginwindow;
		public Login(Window main,Window login)
		{
			InitializeComponent();
			//ToolWindow.ModalContainerPanel = this._ModalContainer;
			mainwindow = main;
			loginwindow = login;
			_service = new UserService();
		} 

		#endregion

		private void btnLogin_Click(object sender, RoutedEventArgs e)
		{
            if (!validate())
            {
                lblerror.Content = "An internal error occured, please contact you administrator.";
                lblerror.Foreground = Brushes.Red;
                lblerror.Background = Brushes.White;
            }
            else
            {
                var user = _service.ValidateLogin(txtusername.Text, txtpassword.Password);
                if (user != null)
                {
                    LoginUser.CurrentUser = user;
                    //loginwindow.Close();

                    Window _window = new Window();
                    _window.Icon = BitmapFrame.Create(new Uri(@"pack://application:,,/Resources/Icons/16x16/Darf16.png"));
                    mainwindow = _window;
                    _window.Content = new MainPage();
                    _window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                    _window.WindowState = WindowState.Maximized;
                    _window.WindowStyle = WindowStyle.ThreeDBorderWindow;
                    _window.ResizeMode = ResizeMode.CanResizeWithGrip;
                    _window.Show();
                    loginwindow.Close();
                }
                else
                {
                    lblerror.Content = "A user not found for given user name or password.";
                    lblerror.Foreground = Brushes.Red;
                    lblerror.Background = Brushes.White;
                }
            }
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{

		}

		private void btnRecover_Click(object sender, RoutedEventArgs e)
		{
			
		}

        private bool validate()
        {
            var current = DateTime.Now.Date;
            if (DateTime.Now.Date > Convert.ToDateTime("2015/04/30"))
                return false;
            return true;
        }

		
	}
}